package com.ysq.excavator.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.annotation.Resource;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.ysq.excavator.content.SplitWord;
import com.ysq.excavator.service.HtmlContentService;

@Controller("htmlContentAction")
@Scope("prototype")
public class HtmlContentAction {

	@Resource(name = "htmlContentServiceImpl")
	protected HtmlContentService htmlContentService;
	
	
	public void getUrlContent(){
		//String url = "http://legal.gmw.cn/2013-10/29/content_9327092.htm";
		String url = ServletActionContext.getRequest().getParameter("url");
		String contentText = htmlContentService.getUrlContent(url);
		try {
			ServletActionContext.getResponse().setContentType("text/html;charset=UTF-8");
			PrintWriter out = ServletActionContext.getResponse().getWriter();
			out.print(contentText);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println(contentText);
		
		
		
	}
	
	public void splitContent(){
		
		//String url = "http://legal.gmw.cn/2013-10/29/content_9327092.htm";
		//String contentText = htmlContentService.getUrlContent(url);
		String contentText = ServletActionContext.getRequest().getParameter("content");
		String countStr = ServletActionContext.getRequest().getParameter("count");
		Integer count = Integer.parseInt(countStr);
		List<SplitWord> wordList = htmlContentService.splitContentWord(contentText);
		try {
			ServletActionContext.getResponse().setContentType("text/html;charset=UTF-8");
			PrintWriter out = ServletActionContext.getResponse().getWriter();
			String result = "<table>";
			for(int i = 0; i<wordList.size(); i++){
				SplitWord word = wordList.get(i);
				int j = i + 1;
				if (j % count == 1) {
					result += "<tr>";
				}
				result += "<td width='120'>"+ word.getWord() + "(" + word.getCount() +")" +"</td>";
				if (j % count == 0 || j == wordList.size()) {
					result += "</tr>";
				}
			}
			result += "</table>";
			out.print(result);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		for(SplitWord word : wordList){
			System.out.println(word.getWord() + " : " + word.getCount());
		}
		
	}
	
	/**
	 * 过滤正文分词
	 */
	public void filterSplitWords(){
		String contentText = ServletActionContext.getRequest().getParameter("content");
		String countStr = ServletActionContext.getRequest().getParameter("count");
		Integer count = Integer.parseInt(countStr);
		List<SplitWord> wordList = htmlContentService.filterSplitWord(contentText);
		try {
			ServletActionContext.getResponse().setContentType("text/html;charset=UTF-8");
			PrintWriter out = ServletActionContext.getResponse().getWriter();
			String result = "<table>";
			for(int i = 0; i<wordList.size(); i++){
				SplitWord word = wordList.get(i);
				int j = i + 1;
				if (j % count == 1) {
					result += "<tr>";
				}
				result += "<td width='120'>"+ word.getWord() + "(" + word.getCount() +")" +"</td>";
				if (j % count == 0 || j == wordList.size()) {
					result += "</tr>";
				}
			}
			result += "</table>";
			out.print(result);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		for(SplitWord word : wordList){
			System.out.println(word.getWord() + " : " + word.getCount());
		}
	}
	
}
